package com.googlecode.agscrum.model.exceptions;

import java.util.Locale;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.NoSuchMessageException;
import org.springframework.context.annotation.Scope;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.stereotype.Component;

import com.googlecode.agscrum.model.exceptions.GScrumDomainException.TypeMessage;

@Component("messageFactory")
@Scope(value = "singleton")
public class MessageFactory {
	@Autowired
	private ReloadableResourceBundleMessageSource messageSource;

	public void setMessageSource(ReloadableResourceBundleMessageSource messageSource) {
		this.messageSource = messageSource;
	}

	public GScrumMessageException createMessage(String keyString,
			String keyDefaultString, TypeMessage typeMessage, Object... objects) {
		Locale ptBr = new Locale("pt", "BR");
		String message = null;
		try {
			message = messageSource.getMessage(keyString, objects, ptBr);
		} catch (NoSuchMessageException e) {
			if (keyDefaultString != null)
				message = messageSource.getMessage(keyDefaultString, objects,
						ptBr);
		}
		return new GScrumMessageException(message, typeMessage);
	}
}
